package initialization

import (
	sf "github.com/bwmarrin/snowflake"
	"go.uber.org/zap"
	"time"
)

var node *sf.Node

// InitSnowFlake 初始化雪花算法
func InitSnowFlake() {
	snowflakeConfig := Config.SnowFlake
	//使用雪花算法生成用户id
	if err := Init(snowflakeConfig.StartTime, snowflakeConfig.MachineId); err != nil {
		SeatLogger.Error("init snowflake is failed", zap.Error(err))
		return
	}
	SeatLogger.Info("初始化雪花算法成功")
}

func Init(startTime string, machineID int64) (err error) {
	var st time.Time
	st, err = time.Parse("2006-01-02", startTime)
	if err != nil {
		return
	}
	sf.Epoch = st.UnixNano() / 1000000
	node, err = sf.NewNode(machineID)
	return
}

func GenUUID() string {
	return node.Generate().String()
}
